package com.fuyao.cloud.personnel.problem.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.entity.ProblemCti;
import com.fuyao.cloud.personnel.api.entity.ProblemInfor;
import com.fuyao.cloud.personnel.problem.service.ProblemCtiService;
import com.fuyao.cloud.personnel.problem.service.ProblemInforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题分类
 *
 * @author pigx code generator
 * @date 2022-04-26 10:13:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cti" )
@Api(value = "cti", tags = "问题分类管理")
public class ProblemCtiController {

    private final ProblemCtiService problemCtiService;

    private final ProblemInforService inforService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param problemCti 问题分类
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('problem_cti_view')")
    public R getFyProblemCtiPage(Page page, ProblemCti problemCti) {
        return R.ok(problemCtiService.page(page, Wrappers.query(problemCti)));
    }

//    /**
//     * 查询树
//     * @param page
//     * @param problemCti
//     * @return
//     */
//    @ApiOperation(value = "分页查询", notes = "分页查询")
//    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('problem_cti_view')")
//    public R getFyProblemCtiPageTree(Page page, ProblemCti problemCti) {
//        return R.ok(problemCtiService.page(page, Wrappers.query(problemCti)));
//    }

    /**
     * 通过id查询问题分类
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_cti_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(problemCtiService.getById(id));
    }

    /**
     * 新增问题分类
     * @param problemCti 问题分类
     * @return R
     */
    @ApiOperation(value = "新增问题分类", notes = "新增问题分类")
    @SysLog("新增问题分类" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('problem_cti_add')" )
    public R save(@RequestBody ProblemCti problemCti) {
        return R.ok(problemCtiService.save(problemCti));
    }

    /**
     * 修改问题分类
     * @param problemCti 问题分类
     * @return R
     */
    @ApiOperation(value = "修改问题分类", notes = "修改问题分类")
    @SysLog("修改问题分类" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('problem_cti_edit')" )
    public R updateById(@RequestBody ProblemCti problemCti) {
        return R.ok(problemCtiService.updateById(problemCti));
    }

    /**
     * 通过id删除问题分类
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除问题分类", notes = "通过id删除问题分类")
    @SysLog("通过id删除问题分类" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('problem_cti_del')" )
    public R removeById(@PathVariable Long id) {
        long inforCount = this.inforService.count(Wrappers.<ProblemInfor>lambdaQuery().eq(ProblemInfor::getCtiId,id));
        if (inforCount > 0){
            return R.failed("删除失败，此分类在问题单中已使用");
        }
        return R.ok(problemCtiService.remove(Wrappers.<ProblemCti>lambdaQuery()
                .eq(ProblemCti::getId,id).or()
                .eq(ProblemCti::getParentId,id)));
    }

    /**
     * 导出excel 表格
     * @param problemCti 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('problem_cti_export')" )
    public List<ProblemCti> export(ProblemCti problemCti) {
        return problemCtiService.list(Wrappers.query(problemCti));
    }


    /**
     * 返回树形菜单集合
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public R getTree() {
        return R.ok(problemCtiService.selectTree());
    }
}
